//
//  RiskEnsureController.m
//  WBBaseProject
//
//  Created by xiaozhi on 2018/4/10.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "RiskEnsureController.h"
#import "NSObject+AFNetworking.h"

#import "XYHomeNavigationView.h"
#import "RiskEnsureHeaderView.h"
#import "WelfareTableViewCell.h"
#import "QuestionViewCell.h"
#import "CCNavigationBarMenu.h"
#import "RefundRiskController.h"
//首页导航栏滚动距离
#define NAVBAR_CHANGE_POINT 50

@interface RiskEnsureController ()<UITableViewDelegate,UITableViewDataSource>
{
    CCNavigationBarMenu *menu;
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) RiskEnsureHeaderView *headView;

/** <#注释#> */
@property (nonatomic, strong) XYHomeNavigationView *naviView;

@end

@implementation RiskEnsureController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.navigationItem.title = @"投资账户";
    [self registerCell];
    [self.view addSubview:self.tableView];
    self.navigationController.navigationBar.hidden = YES;
    
    XYHomeNavigationView *naviView = [[XYHomeNavigationView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavigationBarHeight)];
    _naviView = naviView;
    _naviView.titleLb.text = @"风险保证";
    
    [_naviView.rightBtn setImage:nil forState:UIControlStateNormal];
    [_naviView.rightBtn setTitle:@"..." forState:UIControlStateNormal];
    _naviView.rightBtn.titleLabel.font = WB_FontSize(30);
    _naviView.rightBtn.transform = CGAffineTransformMakeRotation(-M_PI_2);
    [_naviView.rightBtn updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(-15);
        make.bottom.equalTo(0);
    }];
    [_naviView.rightBtn addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
    
    [_naviView.leftBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:naviView];
}
- (void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)showMenu:(UIButton*)btn
{
    NSMutableArray *menuItems = [NSMutableArray array];
    //menuImages = @[@"icon_menu_venue", @"icon_menu_map", @"icon_menu_region", @"icon_menu_search", @"icon_menu_scan"];
    NSArray* menuTitles = @[@"退保证金", @"帮助中心", @"投诉建议"];
    for (int i = 0; i < menuTitles.count; i++) {
        CCNavigationBarMenuItem *item = [CCNavigationBarMenuItem navigationBarMenuItemWithImage:nil title:menuTitles[i]];
        [menuItems addObject:item];
    }
    
    menu = [[CCNavigationBarMenu alloc] initWithOrigin:CGPointMake(kScreenWidth-130, 64) width:120];
    menu.items = menuItems;
    
    [menu show];
    
    WB_WEAKSELF;
    menu.didSelectMenuItem = ^(CCNavigationBarMenu *menu, NSInteger row) {
      
        switch (row) {
            case 0:
            {//退保证金
                
                RefundRiskController *vc = [[RefundRiskController alloc]init];
                vc.type =RefundRiskNomal;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                break;
            }
            case 1:
            {//帮助中心
                break;
            }
            case 2:
            {//投诉建议
                break;
            }
            default:
                break;
        }
    };
}
//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    //[menu dismissaa];
//}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)registerCell
{
    [self.tableView registerNib:[UINib nibWithNibName:@"WelfareTableViewCell" bundle:nil] forCellReuseIdentifier:@"WelfareTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"QuestionViewCell" bundle:nil] forCellReuseIdentifier:@"QuestionViewCell"];
//    [self.tableView registerNib:[UINib nibWithNibName:@"TimeDetailCell" bundle:nil] forCellReuseIdentifier:TimeDetailCellReuseId];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 2;
            break;
        
        default:
            return 0;
            break;
    }
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    
    switch (indexPath.section) {
        case 0:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"WelfareTableViewCell" forIndexPath:indexPath];
            return cell;
            break;
        }
        case 1:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"QuestionViewCell" forIndexPath:indexPath];
            return cell;
            break;
        }
       
        default:
            return cell;
            break;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vi = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    vi.backgroundColor = [UIColor whiteColor];
    
    UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 60)];
    la.font = WB_FontSize(14);
    [vi addSubview:la];
    
    switch (section) {
        case 0:
        {
            
            la.text = @"保障福利";
            
            break;
        }
        case 1:
        {
            la.text = @"常见问题";
            
            break;
        }
        
        default:
            
            break;
    }
    return vi;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
    switch (indexPath.section) {
        case 0:
        {
            height = 140;
            
            break;
        }
        case 1:
        {
            height = 75;
            
            break;
        }
            
        default:
            
            height = 0;
            break;
    }
    
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat cellOffy = scrollView.contentOffset.y;
    if (cellOffy < 0) {
        self.naviView.hidden = YES;
    }else{
        self.naviView.hidden = NO;
    }
    
    if (cellOffy > NAVBAR_CHANGE_POINT) {
        
        [self.naviView changeColorWithState:YES];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleDefault animated:true];
    }else {
        CGFloat alpa = cellOffy/NAVBAR_CHANGE_POINT;
        if (alpa > 0.8) {
            self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, 0.8);
            return ;
        }
        [self.naviView changeColorWithState:NO];
        self.naviView.backgroundColor = WB_RGBAColor(255, 255, 255, alpa);
        [WB_SharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:true];
    }
}


#pragma mark - 懒加载  Lazy Load
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = WB_RGBColor(245, 248, 253);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}

- (RiskEnsureHeaderView *)headView
{
    if (_headView == nil) {
        _headView = [[NSBundle mainBundle] loadNibNamed:@"RiskEnsureHeaderView" owner:self options:nil].firstObject;
        _headView.type = arc4random()%3;
        WB_WEAKSELF;
        _headView.startClick = ^{
        //开启保障
            RefundRiskController *vc = [[RefundRiskController alloc]init];
            vc.type = RefundRiskRecharge;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }
    return _headView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
